import json
import requests
from BloodSpiderModel.spider_tools import common_utils

class DaiLianWanZi:
    def __init__(self):
        # 代练丸子
        self.dailianwanzi_base_url = "https://app-wanzi.lianlianwanzi.com"
        self.spider_common_utils = common_utils.GeneralToolkit()
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Linux; Android 12; 23117RK66C Build/V417IR; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/101.0.4951.61 Mobile Safari/537.36 uni-app Html5Plus/1.0 (Immersed/0.33333334)',
            'Connection': 'Keep-Alive',
            'Content-Type': 'application/json',
        }
        self.params = {
            'signVersion': '1.0.0',
        }
        self.common = {
            'partnerChannel': 'ball_h5guanwang',
            'partner': 'android',
            'partnerVersion': '4.8.3-4.9.17',
            'partnerDevice': '75AD9AD130569F191CCBDED4CCFB2AAC',
            'authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhcHAiOiJmcm9udGVuZCIsImF1ZCI6IjEwNzQ1NjM4NCIsInN1YiI6MTA3NDU2Mzg0LCJpc3MiOiJ3YW56aXByb2QiLCJleHAiOjE3NjE1NDI3MDgsImlhdCI6MTc1ODk1MDcwOCwianRpIjoiZjRjYWYzNWZhOWU3NDJlYjhjYTgwMGQ4YmMzNzUxZDkifQ.PzvNVTjqwY6MVa02CU4BBsABXA8FB2zZSevay6_zo3Q',
            'timestamp': 1759040001754,
        }
    
    # 发布订单
    def publish_order(self, title,amount,hour,securityDeposit,efficiencyDeposit,playerPhone="3837190115",contactPhone="3837190115", gameRegionName='安卓QQ', heroName="", heroNumber="", inscriptionNumber="",
    ):
        """发布订单
        title: 订单标题
        amount: 订单金额
        hour: 订单时长
        securityDeposit: 安全保证金
        efficiencyDeposit: 效率保证金
        playerPhone: 玩家手机号, 默认值为'duyanbz'
        contactPhone: 联系人手机号, 默认值为'18171759943'
        gameRegionName: 游戏区域名称，默认值为'安卓QQ', 可选值是: 安卓微信、苹果QQ、苹果微信
        heroName: 是否指定英雄, 英雄名称，默认值为空字符串, 不指定任何英雄
        heroNumber: 英雄数量, 默认值为空字符串, 不上传
        inscriptionNumber: 铭文数量, 默认值为空字符串, 不上传
        """

        json_data = {
            'hasTrans': 1,
            'gameId': 1,
            'gameServerId': 122,
            'gameServerName': '默认服',
            'gameName': '王者',
            'gameIcon': 'https://files.llwanzi.com/zympYphpKVhaHN1685523686230531.png',
            'gameLevelingTypeId': 1,
            'gameLevelingTypeName': '排位',
            'gameRegionId': 2,
            'gameRegionName': gameRegionName,
            'tempType': 6,
            'heroName': heroName,
            'heroNumber': heroNumber,
            'inscriptionNumber': inscriptionNumber,
            'currentLevel': '',
            'targetLevel': '',
            'multiTaskInfo': [],
            'common': self.common,
            'orderType': 1,
            'gameRegionServer': [
                1,
                2,
                122,
            ],
            'merchantInvoiceEntry': 1,
            'title': title,
            'amount': amount,
            'hour': hour,
            'securityDeposit': securityDeposit,
            'efficiencyDeposit': efficiencyDeposit,
            'takePassword': '',
            'playerPhone': playerPhone,
            'contactPhone': contactPhone,
            'contactQq': '添加QQ扫码上号',
            'requirement': '1、接单30分钟内上号开打并传首图，无图默认未上号，将扣效率保证金；\n2、禁止联系号主说自己是某平台的打手，只能说自己是打手。透露信息、私下联系号主视具体危害扣罚保证金!\n3、代练所得道具归号主所有，不允许私自动用金币，点券，或者其他任何游戏道具；\n4、禁止出现辱骂、开挂、私下交易、盗号、等红线行为；\n5、任何问题保留证据，心平气和好好解决，严禁谩骂不文明沟通，否则一律封号',
            'explain': 'QQ区登录: *,进入游戏,选择“切换账号”-“添加账号”后联系玩家进行辅助验证\n微信区登录:*,联系玩家进行辅助验证;*,登录微信号,再进入游戏点击与微信好友玩即可\n①接单后有疑问可客服中心关键字触发常见问题解答,或者咨询在线客服\n②排位订单上号记得上传好友排行榜的首图,打完上传好友排行榜的完单图。\n③代练期间请勿私自使用号内任何物品,包括钻石、点卷、金币、体验卡等等。\n④切勿使用外挂,不允许打广告、挂机、恶意骂人等恶意行为。\n⑤私自联系或者回复号主以及号主好友(禁止私下交易),违反扣安全保证金',
            'type': 2,
            'time': '',
            'sourceAmount': '0',
            'cardCouponId': '',
            'operation': '2',
            'category': '0',
            'masterId': '',
            'orderSource': '1',
            'payType': 1,
            'channelConfigId': '0',
            'channelConfigType': '0',
            'platformProfit': '',
            'platformId': '',
            'storeType': 3,
            'gameAccount': '扫码上号',
            'gamePassword': '扫码上号',
            'gameRole': '',
            'insureName': '无',
        }
        if gameRegionName == "苹果微信" or gameRegionName == "苹果WX":
            json_data["gameRegionServer"] = [1,3,523]
            json_data["gameRegionName"] = "苹果微信"
            
        elif gameRegionName == "安卓微信" or gameRegionName == "安卓WX":
            json_data["gameRegionServer"] = [1,4,639]
            json_data["gameRegionName"] = "安卓微信"
        elif gameRegionName == "苹果QQ":
            json_data["gameRegionServer"] = [1,1,1]
        elif gameRegionName == "安卓QQ":
            json_data["gameRegionServer"] = [1,2,122]

        # 代表不同的区,比如苹果、微信等
        json_data["gameServerId"] = json_data["gameRegionServer"][-1]

        response = requests.post(
            f'{self.dailianwanzi_base_url}/api/order/action/merchant/store',
            params=self.params,
            headers=self.headers,
            json=json_data,
        ).json()
        if response.get("code") == 10000:
            response["data"]["gameRegionName"] = json_data["gameRegionName"]
        return response


    # 取消订单
    # orderId: 订单ID
    def cancel_order(self, orderId):
        json_data = {
            'tradeNo': orderId,
            'common':self.common
        }

        response = requests.post(
            f'{self.dailianwanzi_base_url}/api/order/action/cancel',
            params=self.params,
            headers=self.headers,
            json=json_data,
        )
        return response.json()
    


    # 查询订单
    # orderId: 订单ID
    def query_order(self, orderId, page=1, pageSize=10):

        json_data = {
            'pageNo': page,
            'pageSize': pageSize,
            'userType': 1,
            'tableType': 0,
            'keyWord': orderId,
            'common': self.common,
        }

        response = requests.post(
            f'{self.dailianwanzi_base_url}/api/order/user/list',
            params=self.params,
            headers=self.headers,
            json=json_data,
        )
        return response.json()
            
    # 获取我的全部订单
    # pageNo: 页码
    # pageSize: 每页数量
    def get_my_orders(self, pageNo=1, pageSize=10):
        json_data = {
            'pageNo': pageNo,
            'pageSize': pageSize,
            'userType': 1,
            'tableType': 0,
            'keyWord': '',
            'common':self.common,
        }

        response = requests.post(
            f'{self.dailianwanzi_base_url}/api/order/user/list',
            params=self.params,
            headers=self.headers,
            json=json_data,
        ).json()
        return response


    # 获取我的余额
    def get_my_balance(self):
        json_data = {
            'common': self.common
        }

        response = requests.post(
            f'{self.dailianwanzi_base_url}/api/user/balance',
            params=self.params,
            headers=self.headers,
            json=json_data,
        )
        return response.json()


    # 获取订单列表-v2版本：支持按照状态获取
    # status: 订单状态
    def get_order_list_v2(self, tableType=0, pageNo=1, pageSize=10):
        """
        "tableList": [
            {
                "tableType": 0,
                "tableName": "全部",
            },
            {
                "tableType": 101,
                "tableName": "待付待接",
            },
            {
                "tableType": 102,
                "tableName": "代练中",
            },
            {
                "tableType": 103,
                "tableName": "待验收",
            },
            {
                "tableType": 104,
                "tableName": "撤销中",
            },
            {
                "tableType": 105,
                "tableName": "仲裁中",
            },
            {
                "tableType": 106,
                "tableName": "已撤销",
            },
            {
                "tableType": 107,
                "tableName": "已仲裁",
            },
            {
                "tableType": 108,
                "tableName": "已结算",
            },
            {
                "tableType": 109,
                "tableName": "异常中",
            },
            {
                "tableType": 110,
                "tableName": "锁定中",
            },
            {
                "tableType": 111,
                "tableName": "已取消",
            }
            ]
        }
        """
        json_data = {
            'pageNo': pageNo,
            'pageSize': pageSize,
            'userType': 1,
            'tableType': tableType,
            'keyWord': '',
            'common':self.common,
        }

        response = requests.post(
            f'{self.dailianwanzi_base_url}/api/order/user/list',
            params=self.params,
            headers=self.headers,
            json=json_data,
        ).json()
        return response


if __name__ == "__main__":
    dailianwanzi = DaiLianWanZi()
    # print(dailianwanzi.publish_order(title="10.6 张飞 河北省标 战力4225 排位钻石1 上榜6400 无标0元结算", amount=3, hour=48, securityDeposit=1, efficiencyDeposit=1))
    # print(dailianwanzi.cancel_order("WZ20250928082620324281"))
    print(dailianwanzi.get_my_balance())